package com.concerneyes.mine.TabActivity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.Task.MyInfoImpl;
import com.concerneyes.mine.Task.MyInfoListener;
import com.concerneyes.mine.model.MyInfoModel;
import com.concerneyes.pay.ConfirmInfoActivity;

import org.w3c.dom.Text;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import common.AbsActivity;
import common.PreferUtil;
import util.ActivityUtil;
import util.ImageUtil;
import util.Util;
import view.CircleImageView;
import view.MyUniversalDialog;
import wheelview.ChangeBirthDialog;

/**
 * 个人中心
 */

public class MyInfo extends AbsActivity implements View.OnClickListener, MyInfoListener {
    private ImageView ivGone;
    private FrameLayout flEditInfo;
    private ScaleImageActivity scaleView;
    private CircleImageView mIvHeader;
    private RelativeLayout mRlPhoto;
    private TextView mName, mTvName, mTvSex, mTvBirthday, mTvPhone, mTvZhiye;
    private RelativeLayout mRlName, mRlSex, mRlBirthday, mRlPhone, mRlZhiye, mRlAdress;
    private LinearLayout mLlMyselfContact;
    private MyUniversalDialog dialog;
    private MyInfoImpl impl;
    private MyInfoModel model;
    private boolean isMan = true;
    final int DATE_DIALOG = 1;
    int mYear, mMonth, mDay;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.mine_myself_info);
        setTitle(R.string.my_centre);
        initView();
    }

    private void initView() {
        ivGone = (ImageView) findViewById(R.id.iv_gone);
        flEditInfo = (FrameLayout) findViewById(R.id.fl_edit_info);
        mIvHeader = (CircleImageView) findViewById(R.id.iv_header);
        mRlPhoto = (RelativeLayout) findViewById(R.id.rl_photo);
        mName = (TextView) findViewById(R.id.name);
        mTvName = (TextView) findViewById(R.id.tv_name);
        mRlName = (RelativeLayout) findViewById(R.id.rl_name);
        mTvSex = (TextView) findViewById(R.id.tv_sex);
        mRlSex = (RelativeLayout) findViewById(R.id.rl_sex);
        mTvBirthday = (TextView) findViewById(R.id.tv_birthday);
        mRlBirthday = (RelativeLayout) findViewById(R.id.rl_birthday);
        mTvPhone = (TextView) findViewById(R.id.tv_phone);
        mRlPhone = (RelativeLayout) findViewById(R.id.rl_phone);
        mTvZhiye = (TextView) findViewById(R.id.tv_zhiye);
        mRlZhiye = (RelativeLayout) findViewById(R.id.rl_zhiye);
        mRlAdress = (RelativeLayout) findViewById(R.id.rl_adress);
        mLlMyselfContact = (LinearLayout) findViewById(R.id.ll_myself_contact);
        ivGone.setOnClickListener(this);
        mIvHeader.setOnClickListener(this);
        mRlPhoto.setOnClickListener(this);
        mRlName.setOnClickListener(this);
        mRlSex.setOnClickListener(this);
        mRlBirthday.setOnClickListener(this);
        mRlPhone.setOnClickListener(this);
        mRlZhiye.setOnClickListener(this);
        mRlAdress.setOnClickListener(this);
        impl = new MyInfoImpl(this);
        impl.myInfoImpl();
    }

    @Override
    protected void onResume() {
        super.onResume();
        impl.myInfoImpl();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_gone:
                flEditInfo.setVisibility(View.GONE);
                break;
            case R.id.iv_header:
                // 查看头像大图
                String header;
                if (TextUtils.isEmpty(PreferUtil.getHeaderImgUrl())) {
                    header = CEApp.getResLink(model.getPortrait());
                } else {
                    header = CEApp.getResLink(PreferUtil.getHeaderImgUrl());
                }
                showBigImage(header);
                break;
            case R.id.rl_phone:
                editInfo(getResources().getString(R.string.my_phone));
                break;
            case R.id.rl_name:
                editInfo(getResources().getString(R.string.my_name));
                break;
            case R.id.rl_sex:
                dialogSet(true);
                break;
            case R.id.rl_birthday:
                Calendar c = Calendar.getInstance();
                int years = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH) + 1;
                int day = c.get(Calendar.DAY_OF_MONTH);
                ChangeBirthDialog mChangeBirthDialog = new ChangeBirthDialog(
                        MyInfo.this);
                mChangeBirthDialog.setDate(years, month, day);
                mChangeBirthDialog.show();
                mChangeBirthDialog.setBirthdayListener(new ChangeBirthDialog.OnBirthListener() {

                    @Override
                    public void onClick(String year, String month, String day) {
                        Toast.makeText(MyInfo.this,
                                year + "-" + month + "-" + day,
                                Toast.LENGTH_LONG).show();
                    }
                });
                break;
            case R.id.rl_zhiye:
                editInfo(getResources().getString(R.string.my_zhiye));
                break;
            case R.id.rl_adress:
                Bundle bundle = new Bundle();
                bundle.putString("title", getResources().getString(R.string.my_adress_));
                ActivityUtil.setIntent(this, MyAdressActivity.class, bundle);
                break;
        }

    }

    private void dialogSet(boolean isSex) {
        int width = this.getWindowManager().getDefaultDisplay().getWidth();
        int hight = this.getWindowManager().getDefaultDisplay().getHeight();
        dialog = new MyUniversalDialog(this);
        dialog.setCanceledOnTouchOutside(true);
        if (isSex) {
            dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_pay, null),
                    MyUniversalDialog.DialogGravity.CENTER, -2, 4 * width / 5);
        } else {
            dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_date, null),
                    MyUniversalDialog.DialogGravity.CENTER, -2, 5 * width / 6);
            TextView title = (TextView) dialog.findViewById(R.id.title);
            DatePicker data_picker = (DatePicker) dialog.findViewById(R.id.data_picker);
            RelativeLayout rl_title = (RelativeLayout) dialog.findViewById(R.id.rl_title);
            rl_title.setVisibility(View.GONE);
            title.setText(R.string.set_birthday);
            dialog.show();
            return;
        }
        RelativeLayout rl_man = (RelativeLayout) dialog.findViewById(R.id.rl_wechat);
        RelativeLayout rl_woman = (RelativeLayout) dialog.findViewById(R.id.rl_alipay);
        final ImageView iv_woman = (ImageView) dialog.findViewById(R.id.iv_alipay);
        final ImageView iv_man = (ImageView) dialog.findViewById(R.id.iv_wechat);

        final TextView tvChooseSex = (TextView) dialog.findViewById(R.id.tv_pay);
        final ImageView cancel = (ImageView) dialog.findViewById(R.id.pay_cancel);
        ImageView iv1 = (ImageView) dialog.findViewById(R.id.iv_1);
        ImageView iv2 = (ImageView) dialog.findViewById(R.id.iv_2);
        TextView tv1 = (TextView) dialog.findViewById(R.id.tv_1);
        TextView tv2 = (TextView) dialog.findViewById(R.id.tv_2);
        TextView title = (TextView) dialog.findViewById(R.id.title);
        TextView tv_pay_info = (TextView) dialog.findViewById(R.id.tv_pay_info);
        iv1.setImageDrawable(getResources().getDrawable(R.drawable.ico_male));
        iv2.setImageDrawable(getResources().getDrawable(R.drawable.ico_female));
        tv1.setText(R.string.man);
        tv2.setText(R.string.woman);
        title.setText(R.string.set_sex);
        tv_pay_info.setVisibility(View.GONE);
        dialog.show();
        rl_man.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                iv_woman.setImageResource(R.drawable.btn_unselected);
                iv_man.setImageResource(R.drawable.btn_selected);
                isMan = true;
            }
        });
        rl_woman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_woman.setImageResource(R.drawable.btn_selected);
                iv_man.setImageResource(R.drawable.btn_unselected);
                isMan = false;
            }
        });
        tvChooseSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMan) {
                    model.setGender("男");
                } else {
                    model.setGender("女");
                }
                impl.myInfoUpdateImpl(model);
                dialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    private void editInfo(String string) {
        Bundle bundle = new Bundle();
        bundle.putString("title", string);
        if (model != null) {
            bundle.putSerializable("MyInfoModel", model);
        }
        ActivityUtil.setIntent(this, EditInfoActivity.class, bundle);
    }


    private void showBigImage(String url) {
        Bundle bundle = new Bundle();
        bundle.putString("imgUrl", url);
        ActivityUtil.setIntent(this, ScaleImageActivity.class, bundle);
    }

    @Override
    public void onMyInfoSuccess(MyInfoModel data) {
        this.model = data;
        setData(data);
    }

    private void setData(MyInfoModel data) {
        String resLink = CEApp.getResLink(data.getPortrait());
        if (TextUtils.isEmpty(PreferUtil.getHeaderImgUrl())) {
            ImageUtil.glideloadImage(resLink, mIvHeader, R.drawable.default_header);
        } else {
            ImageUtil.glideloadImage(PreferUtil.getHeaderImgUrl(), mIvHeader, R.drawable.default_header);
        }
        if (!TextUtils.isEmpty(data.getNickname())) {
            mTvName.setText(data.getNickname());
        }
        if (!TextUtils.isEmpty(data.getGender())) {
            mTvSex.setText(data.getGender());
        }
        if (data.getAge() > 0) {
            mTvBirthday.setText(String.valueOf(data.getAge()));
        }
        if (!TextUtils.isEmpty(data.getPhone())) {
            mTvPhone.setText(data.getPhone());
        }
        if (!TextUtils.isEmpty(data.getJob())) {
            mTvZhiye.setText(data.getJob());
        }
    }

    @Override
    public void onMyInfoFail() {

    }

    @Override
    public void onMyInfoUpdateSuccess(MyInfoModel data) {
        this.model = data;
    }

    @Override
    public void onMyInfoUpdateFail() {

    }
}
